<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工作室</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            text-align: center;
            padding: 20px;
        }

        h1 {
            color: #333;
        }

        #password {
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-bottom: 10px;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin: 0 10px;
        }

        button:hover {
            background-color: #45a049;
        }

        .loader {
            border: 16px solid #f3f3f3;
            border-radius: 50%;
            border-top: 16px solid #4CAF50;
            width: 120px;
            height: 120px;
            animation: spin 2s linear infinite;
            display: none;
            margin: 20px auto;
        }

        @keyframes spin {
            0% {
                transform: rotate(0deg);
            }

            100% {
                transform: rotate(360deg);
            }
        }

        .loading-text {
            display: none;
            font-size: 18px;
            color: #333;
            margin-top: 10px;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgb(0, 0, 0);
            background-color: rgba(0, 0, 0, 0.4);
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 400px;
            text-align: center;
            border-radius: 10px;
        }

        #imageButton {
            cursor: pointer;
            margin-bottom: 10px;
            width: 100px;
            height: 100px;
            object-fit: cover;
        }

        .countdown-modal {
            font-size: 18px;
            color: #333;
        }
    </style>
    <script>
        let clickCount = 0; 
        let bypassPassword = false; 
        let canClick = true; 

        document.onkeydown = function (event) {
            // 禁止某些键盘快捷键
            const forbiddenKeys = [
                { ctrl: true, shift: true, keyCode: 73 }, 
                { ctrl: true, shift: true, keyCode: 74 }, 
                { ctrl: true, keyCode: 85 }, 
                { keyCode: 123 } 
            ];
            if (forbiddenKeys.some(k => k.keyCode === event.keyCode && (!k.ctrl || event.ctrlKey) && (!k.shift || event.shiftKey))) {
                event.preventDefault();
            }
        };

        document.addEventListener('contextmenu', e => e.preventDefault());

        function setDeviceState(device, state) {
            if (!canClick) {
                showCountdownModal();
                return;
            }

            const passwordInput = bypassPassword ? '' : document.getElementById('password').value;

            if (!bypassPassword && passwordInput === '') {
                alert('密码错误，请重试');
                return;
            }

            canClick = false;
            toggleLoader(true);

            fetch('api.php', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: new URLSearchParams({
                    device,
                    state,
                    password: passwordInput
                })
            })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        showModal(`${device === 'door' ? '门' : '门'}已${state === 'on' ? '打开' : '关闭'}`);
                    } else {
                        alert(data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showModal('操作失败');
                })
                .finally(() => {
                    toggleLoader(false);
                    setTimeout(() => { canClick = true; }, 2000);
                });
        }

        function toggleLoader(show) {
            const loader = document.querySelector('.loader');
            const loadingText = document.querySelector('.loading-text');
            loader.style.display = show ? 'block' : 'none';
            loadingText.style.display = show ? 'block' : 'none';
        }

        function showModal(message) {
            const modal = document.getElementById('myModal');
            const modalContent = document.getElementById('modalContent');
            modalContent.innerText = message;
            modal.style.display = 'flex';

            setTimeout(() => { modal.style.display = 'none'; }, 2000);
        }

        function handleImageClick() {
            clickCount++;
            if (clickCount === 3) {
                document.getElementById('password').style.display = 'none';
                bypassPassword = true;
                canClick = true;
                clickCount = 0;
            }
        }

        // 显示操作倒计时模态框
        function showCountdownModal() {
            const countdownModal = document.createElement('div');
            countdownModal.classList.add('modal');
            countdownModal.id = 'countdownModal';

            const countdownContent = document.createElement('div');
            countdownContent.classList.add('modal-content');
            countdownContent.classList.add('countdown-modal');
            countdownContent.id = 'countdownContent';
            countdownModal.appendChild(countdownContent);

            document.body.appendChild(countdownModal);
            countdownModal.style.display = 'flex';

            let countdown = 5;
            const countdownInterval = setInterval(() => {
                countdownContent.innerText = `请等待 ${countdown} 秒后再操作`;
                countdown--;

                if (countdown < 0) {
                    clearInterval(countdownInterval);
                    countdownModal.style.display = 'none';
                    document.body.removeChild(countdownModal);
                }
            }, 1000);
        }
    </script>
</head>

<body>
    <img id="imageButton" src="1.jpg" onclick="handleImageClick()">
    <h1>开关门</h1>
    <label for="password">请输入密码</label><br>
    <input type="password" id="password" name="password"><br><br>
    <button onclick="setDeviceState('door', 'on')">开门</button>
    <button onclick="setDeviceState('door', 'off')">关门</button><br><br>
    <button onclick="setDeviceState('light', 'on')">暂停</button>
    <div class="loader"></div>
    <div class="loading-text">加载中...</div>

    <div id="myModal" class="modal">
        <div class="modal-content" id="modalContent"></div>
    </div>
</body>

</html>